package com.khshop.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khshop.pojo.Comment;
import com.khshop.mapper.CommentMapper;
import com.khshop.pojo.Order;
import com.khshop.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.khshop.vo.ServiceResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-15
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired(required = false)
    CommentMapper commentMapper;

    @Override
    public ServiceResult<Comment> addComment(Comment comment) {
        return ServiceResult.of(commentMapper.insert(comment)>0);
    }

    @Override
    public ServiceResult<IPage<Comment>> showComment(Integer page, Integer limit) {
        Page<Comment> iPage = new Page<Comment>(page, limit);
        IPage<Comment> orderIPage = commentMapper.selectPage(iPage, null);
        return ServiceResult.of(orderIPage,orderIPage.getSize()>0);
    }

    @Override
    public ServiceResult<Comment> delComment(Long[] id) {
        Comment comment = new Comment();
        comment.setIsdel(true);
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", Arrays.asList(id));
        return ServiceResult.of(commentMapper.update(comment,queryWrapper)>0);
    }

    @Override
    public ServiceResult<Comment> replyComment(Long id, String newComment) {
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        commentQueryWrapper.eq("id",id);
        Comment comment = commentMapper.selectOne(commentQueryWrapper);
        Map<String,String> map=new HashMap<>();
        map.put("oldComment",comment.getConent());
        map.put("newComment",newComment);
        String jsonString = JSON.toJSONString(map);
        comment.setId(id);
        comment.setConent(jsonString);
        return ServiceResult.of(commentMapper.updateById(comment)>0);
    }

    @Override
    public ServiceResult<IPage<Comment>> searchComment(Map<String,Object> map, Integer page, Integer limit) {
        QueryWrapper<Comment> commentQueryWrapper = new QueryWrapper<>();
        for (String key:map.keySet()){
            commentQueryWrapper=commentQueryWrapper.like(key,map.get(key));
        }
        Page<Comment> iPage = new Page<Comment>(page, limit);
        IPage<Comment> orderIPage = commentMapper.selectPage(iPage, commentQueryWrapper);
        return ServiceResult.of(orderIPage,orderIPage.getSize()>0);
    }

    @Override
    public ServiceResult<Comment> checkComment(Long id) {
        return null;
    }
}
